package logic

import (
	"context"
	"projectes/models/redis"

	"projectes/projectrpc/internal/svc"
	"projectes/projectrpc/projectrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectlistLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectlistLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectlistLogic {
	return &CollectlistLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectlistLogic) Collectlist(in *projectrpc.CollectlistRequest) (*projectrpc.CollectlistResponse, error) {
	// todo: add your logic here and delete this line

	scores := redis.Client.ZRevRangeWithScores("collect", 0, 9)

	var sliceStruct []*projectrpc.CollectlistStruct

	for _, listStruct := range scores.Val() {
		list := &projectrpc.CollectlistStruct{
			ProductName: listStruct.Member.(string),
			Score:       int64(listStruct.Score),
		}

		sliceStruct = append(sliceStruct, list)
	}

	return &projectrpc.CollectlistResponse{List: sliceStruct}, nil
}
